package com.xxcxcxcx.xc_moremob.common.skills.ThrownPotion;

import com.xxcxcxcx.xc_moremob.common.entities.monster.realization.SkillBasicEntity;
import com.xxcxcxcx.xc_moremob.common.skills.Currentskills;
import com.xxcxcxcx.xc_moremob.common.skills.UseskillGoal;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ThrownPotion;
import net.minecraft.world.entity.raid.Raider;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.phys.Vec3;

public class ThrownPotionSkillGoal extends UseskillGoal {
    private final TargetingConditions vexCountTargeting = TargetingConditions.forNonCombat().range(16.0D).ignoreLineOfSight().ignoreInvisibilityTesting();
    public int ContinuousSkills;
    public int SkillsCount;
    public int RepetitionNum;
    public int RepetitionInterval;
    public ThrownPotionSkillGoal(SkillBasicEntity mo) {
        this(mo,20,50,50,5,4);
    }
    public boolean canContinueToUse() {
        LivingEntity livingentity = mob.getTarget();
        return livingentity != null && livingentity.isAlive() && this.SkillsCount > 0;
    }
    public ThrownPotionSkillGoal(SkillBasicEntity mo, int CastWarmup, int Casting, int CastingInterval,int repetitionNum,int repetitionInterval){
        super(mo,CastWarmup,Casting,CastingInterval);
        ContinuousSkills=repetitionNum*repetitionInterval;
        RepetitionNum=repetitionNum;
        RepetitionInterval=repetitionInterval;
    }
    public void start() {
        super.start();
        SkillsCount=ContinuousSkills;
    }
    public void tick() {
        if(this.attackWarmupDelay != 0)--this.attackWarmupDelay;
        if (this.attackWarmupDelay == 0){
            if(SkillsCount%RepetitionInterval == 0){
                this.performSpellCasting();
                mob.playSound(mob.getCastingSoundEvent(), 1.0F, 1.0F);
            }
            this.SkillsCount--;
        }
    }
    protected void performSpellCasting() {
        LivingEntity p_34143_=mob.getTarget();
        Vec3 vec3 = p_34143_.getDeltaMovement();
        Potion potion = mob.level.getRandom().nextFloat()>0.1f? Potions.HARMING:Potions.STRONG_HARMING;
        if (p_34143_ instanceof Raider) {
            if (p_34143_.getHealth() <= 4.0F) {
                potion = Potions.HEALING;
            } else {
                potion = Potions.REGENERATION;
            }
            mob.setTarget((LivingEntity)null);
        } else if (!p_34143_.hasEffect(MobEffects.MOVEMENT_SLOWDOWN)) {
            potion = Potions.SLOWNESS;
        } else if (p_34143_.getHealth() >= (potion==Potions.HARMING?8.0F:16.0F) && !p_34143_.hasEffect(MobEffects.POISON)) {
            potion = Potions.POISON;
        } else if (!p_34143_.hasEffect(MobEffects.WEAKNESS) && mob.level.getRandom().nextFloat() < 0.25F) {
            potion = Potions.WEAKNESS;
        }
        ThrownPotion thrownpotion = new ThrownPotion(mob.level, mob);
        thrownpotion.setPos(mob.getX(),mob.getY()+3,mob.getZ());
        thrownpotion.setItem(PotionUtils.setPotion(new ItemStack(Items.SPLASH_POTION), potion));
        thrownpotion.setXRot(thrownpotion.getXRot() - -20.0F);
        double d0 = p_34143_.getX() + vec3.x - mob.getX();
        double d1 = p_34143_.getEyeY() - (double)1.1F - mob.getY() - 3;
        double d2 = p_34143_.getZ() + vec3.z - mob.getZ();
        double d3 = Math.sqrt(d0 * d0 + d2 * d2);
        thrownpotion.shoot(d0, d1 + d3 * 0.2D, d2, 0.75F, 8.0F);
        if (!mob.isSilent()) {
            mob.level.playSound((Player)null, mob.getX(), mob.getY() + 3, mob.getZ(), SoundEvents.WITCH_THROW, mob.getSoundSource(), 1.0F, 0.8F + mob.level.getRandom().nextFloat() * 0.4F);
        }
        mob.level.addFreshEntity(thrownpotion);
    }
    protected SoundEvent getSpellPrepareSound() {
        return SoundEvents.EVOKER_PREPARE_SUMMON;
    }
    protected Currentskills getSpell() {
        return Currentskills.THROWN;
    }
}